自定義 Python 模組


在介紹自定義 Python 套件/模組時,讓我們舉個實際例子說明會比較清楚。

以下我們在 main.py 檔案同一層新增一個模組檔案 my_module.py,讓 main.py 主程式可以讀取使用:

my_module.py:
def add_num(num1, num2):
    return num1 + num2
main.py:
# 引用模組
from my_module import add_num
# 呼叫程式
result = add_num(1, 7)
print(result)
8






你可能感興趣的文章

重載父類別元素 (當類別裡含有自己的類別List的時候)

重載父類別元素 (當類別裡含有自己的類別List的時候)

單元測試的藝術, 2/e (The Art of Unit Testing: with examples in C#,  2/e)

單元測試的藝術, 2/e (The Art of Unit Testing: with examples in C#, 2/e)

Arweave 介紹

Arweave 介紹






留言討論